package messages.encrypted;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;

import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SealedObject;

import messages.Message;

/**
 * Encrypted Message.
 * 
 * @author hb
 * 
 */
public class EncryptedMessage extends Message
{

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;

    private SealedObject sObj;

    public EncryptedMessage( Message msg, PublicKey publicKey )
            throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, IOException {
        Cipher cipher = Cipher.getInstance( "RSA" );
        cipher.init( Cipher.ENCRYPT_MODE, publicKey );
        setsObj( new SealedObject( msg, cipher ) );
    }

    /**
     * @param sObj
     *            the sObj to set
     */
    public void setsObj( SealedObject sObj ) {
        this.sObj = sObj;
    }

    /**
     * @return the sObj
     */
    public SealedObject getsObj() {
        return sObj;
    }

    @Override
    public void show() {
        System.out
                .println( "This is an Encrypted Message That you cannot see inside." );
    }
}
